/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.lizg.View;

import cn.lizg.Controller.MessReceive;
import cn.lizg.util.UiUtil;
import java.io.File;
import java.net.Inet4Address;
import java.net.UnknownHostException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

/**
 *
 * @author LizG
 */
public class Receive_JFr extends javax.swing.JFrame {

    /**
     * Creates new form Receive_JFr
     */
    private static String path = null;
    public static boolean cancle = false;

    public Receive_JFr() {
        initComponents();
        UiUtil.setFrameCenter(this);
         UiUtil.setFrameImage(this);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        log_jTA = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        recpath_jTF = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        log_jTA.setEditable(false);
        log_jTA.setColumns(20);
        log_jTA.setRows(5);

        jLabel1.setText("log:");

        recpath_jTF.setEditable(false);
        recpath_jTF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                recpath_jTFActionPerformed(evt);
            }
        });

        jButton1.setText("设置接收路径");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("转为发送");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(recpath_jTF, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton1))
                            .addComponent(jLabel1)
                            .addComponent(log_jTA)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(142, 142, 142)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(22, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(8, 8, 8)
                .addComponent(log_jTA, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(recpath_jTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 100, Short.MAX_VALUE)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(19, 19, 19))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void recpath_jTFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_recpath_jTFActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_recpath_jTFActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        JFileChooser jfc = new JFileChooser();
        String AbsolutePath = null;
        jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        jfc.showDialog(new JLabel(), "选择");
        File file = jfc.getSelectedFile();
        if (file.isDirectory()) {
            // System.out.println("文件夹:"+file.getAbsolutePath());  
            AbsolutePath = file.getAbsolutePath();
        } else if (file.isFile()) {
            //System.out.println("文件:"+file.getAbsolutePath());  
            AbsolutePath = file.getAbsolutePath();
        }

        this.recpath_jTF.setText(AbsolutePath);
        this.path = this.recpath_jTF.getText().trim();
        System.out.println("Receive_JFr135:" + path);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        Ip_JFr ip_JFr = new Ip_JFr();
        ip_JFr.setIp_JFrV(true);
        UiUtil.setFrameappearence();
        System.gc();
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public void setReceive_JFr(boolean flag) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Receive_JFr.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Receive_JFr.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Receive_JFr.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Receive_JFr.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        MessReceive mr = new MessReceive();

        /* Create and display the form */
        System.out.println("setReceive_JFr");
        System.out.println("Receive_JFr168:" + path);
        new Thread(new Runnable() {
            @Override
            public void run() {
              
                try { 
                    while(!cancle){
                    System.out.println("接收Mess中");
//                    Scanner sc = new Scanner(System.in);
//                    int i = sc.nextInt();
//                    System.out.println("Receive_JFr168:" + path);
                    mr.receiveMess();
                    System.out.println("接收Mess完成");
                    int index = mr.getMess().indexOf("$");
                    String filename = mr.getMess().substring(index+1);
                    System.out.println("ip:" + mr.getMess().substring(5,index)  + "  path:" + path);
                        System.out.println("filename" + filename);
                    mr.setIp((String) Inet4Address.getLocalHost().getHostAddress());//？？？
                    System.out.println("启动接收信息进程");
                    if (mr.getMess().substring(0, 5).equals("@send")) {
                        System.out.println("启动弹出确定接口");
                        SureRec_JFr sureRec_JFr = new SureRec_JFr(mr.getMess().substring(5,index), path,filename);
                        System.out.println("打开确定窗口");
                        sureRec_JFr.setSure_JFrV(true);
                        }
                    }
                } catch (UnknownHostException ex) {
                    Logger.getLogger(Receive_JFr.class.getName()).log(Level.SEVERE, null, ex);
                    System.out.println("Receive_JFr167");
                }
            }
        }).start();
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Receive_JFr().setVisible(flag);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextArea log_jTA;
    private javax.swing.JTextField recpath_jTF;
    // End of variables declaration//GEN-END:variables
}
